package com.mmall.common;


import com.google.common.cache.CacheBuilder;
import com.google.common.cache.CacheLoader;
import com.google.common.cache.LoadingCache;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;

import java.util.concurrent.ExecutionException;
import java.util.concurrent.TimeUnit;

public class TokenCache {

    public static final String TOKEN_PREFIX = "token-";

    private static Logger logger = LoggerFactory.getLogger(TokenCache.class);
    private static LoadingCache<String,String> localCache
            = CacheBuilder.newBuilder().initialCapacity(1000)
                .maximumSize(10000)
                .expireAfterAccess(12, TimeUnit.HOURS)
                .build(new CacheLoader<String, String>() {
                    @Override
                    public String load(String s) throws Exception {
                        return "null";
                    }
                });
    public static void setKey(String key, String val){
        localCache.put(key,val);
    }
    public static String getVal(String key){
        String val = null;
        try {
            val = localCache.get(key);
            if("null".equals(val)){
                return null;
            }
            return val;
        } catch (ExecutionException e) {
            logger.error("localCache get error", e);
        }
        return null;
    }
}
